package com.huikeportal.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.controller.common.Log;
import com.huikeportal.controller.common.MD5Util;
import com.huikeportal.controller.common.MessageManager;
import com.huikeportal.dao.AspnetMembershipMapper;
import com.huikeportal.dao.AspnetUsersInRolesMapper;
import com.huikeportal.dao.AspnetUsersMapper;
import com.huikeportal.dao.LoginMapper;
import com.huikeportal.dao.PrivilegeUserDataMapper;
import com.huikeportal.dao.PrivilegeUsersBDDetailMapper;
import com.huikeportal.dao.PrivilegeUsersSiteMapper;
import com.huikeportal.dao.UserAPIMapper;
import com.huikeportal.dao.UserManagerMapper;
import com.huikeportal.model.AspnetMembership;
import com.huikeportal.model.AspnetUsers;
import com.huikeportal.model.AspnetUsersInRoles;
import com.huikeportal.model.ChainSiteInfo;
import com.huikeportal.model.DataTypeIndex;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.service.UserAPIService;

@Service("userAPIService")
public class UserAPIServiceImpl implements UserAPIService {

	@Autowired
	private LoginMapper loginMapper;

	@Autowired
	private UserAPIMapper userAPIMapper;

	@Autowired
	private UserManagerMapper userManagerMapper;

	@Autowired
	private AspnetUsersMapper aspnetUsersMapper;

	@Autowired
	private AspnetMembershipMapper aspnetMembershipMapper;

	@Autowired
	private AspnetUsersInRolesMapper aspnetUsersInRolesMapper;

	@Autowired
	private PrivilegeUsersSiteMapper privilegeUsersSiteMapper;

	@Autowired
	private PrivilegeUserDataMapper privilegeUserDataMapper;

	@Autowired
	private PrivilegeUsersBDDetailMapper privilegeUsersBDDetailMapper;

	// 新增用户提交
	public void addUser(UserManagerInfo userManagerInfo) throws Exception {
		// 插入aspnet_user表
		AspnetUsers aspnetUsers = new AspnetUsers();
		aspnetUsers.setApplicationid(MessageManager.getMsg("application"));
		aspnetUsers.setUsername(userManagerInfo.getUserName().trim());
		aspnetUsers.setLoweredusername(userManagerInfo.getUserName().trim()
				.toLowerCase());
		aspnetUsers.setIsanonymous(false);// 是否匿名，默认为否
		aspnetUsers.setUsertype(2);// 是否收费，默认为2（免费）,现在用不到
		aspnetUsers.setMobilealias(null);// 不知道什么字段，默认空
		aspnetUsers.setCreateUserName(userManagerInfo.getCreateUserId().trim());
		aspnetUsers.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		aspnetUsers.setLastactivitydate(new Date());
		// 取当前客户的上级客户
		UserInfo topInfo = new UserInfo();
		topInfo.setUserName(userManagerInfo.getCreateUserId().trim());
		topInfo = loginMapper.getUserByName(topInfo);
		// 如果创建者是运维账号，则创建的该账号也是运维账号
		if ("2".equals(topInfo.getUserType())) {
			aspnetUsers.setCustomerType("2");
		}
		aspnetUsersMapper.insertSelective(aspnetUsers);
		// 插入aspnet_Membership表
		AspnetMembership aspnetMembership = new AspnetMembership();
		aspnetMembership.setApplicationid(MessageManager.getMsg("application"));
		aspnetMembership.setUserid(aspnetUsers.getUserid());
		aspnetMembership.setPassword(MD5Util.GetMD5Code("123456"));
		aspnetMembership.setPasswordformat(0);// 现在不用了
		aspnetMembership.setPasswordsalt("0");// 现在不用了
		aspnetMembership.setEmail(userManagerInfo.getEmail().trim());
		aspnetMembership.setLoweredemail(userManagerInfo.getEmail().trim()
				.toLowerCase());
		aspnetMembership.setIsapproved(false);// 现在不用了
		aspnetMembership.setIslockedout(false);// 是否锁定，默认不锁定
		aspnetMembership.setCreatedate(new Date());
		aspnetMembership.setLastlogindate(new Date());
		aspnetMembership.setLastpasswordchangeddate(new Date());
		aspnetMembership.setLastlockoutdate(new Date());// 上次锁定时间
		aspnetMembership.setFailedpasswordattemptcount(0);// 现在不用了
		aspnetMembership.setFailedpasswordattemptwindowstart(new Date());// 现在不用了
		aspnetMembership.setFailedpasswordanswerattemptcount(0);// 现在不用了
		aspnetMembership.setFailedpasswordanswerattemptwindowstart(new Date());// 现在不用了
		aspnetMembership.setPhoneno(userManagerInfo.getTel().trim());
		aspnetMembership.setMobilepin(userManagerInfo.getPhone().trim());
		aspnetMembership.setComment(userManagerInfo.getNote().trim());
		aspnetMembership.setRealname(userManagerInfo.getRealName().trim());
		aspnetMembership.setAddress(null);// 暂时没用到
		aspnetMembership.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		aspnetMembership.setCustomerStatus("0");// 未开通
		aspnetMembershipMapper.insertSelective(aspnetMembership);
		// 插入aspnet_UsersInRoles表
		AspnetUsersInRoles aspnetUsersInRoles = new AspnetUsersInRoles();
		aspnetUsersInRoles.setUserid(aspnetUsers.getUserid());
		aspnetUsersInRoles.setRoleid(userManagerInfo.getRoleId());
		aspnetUsersInRoles.setCreatetime(new Date());
		aspnetUsersInRoles.setSource(Integer.parseInt(MessageManager
				.getMsg("insertSource")));
		aspnetUsersInRolesMapper.insertSelective(aspnetUsersInRoles);
		MessageCommon message = new MessageCommon();
		message.setFlag(true);
		message.setMessage("新增用户成功！");
		// 插入日志表
		Log log = Log.getLogger();
		log.logger.info("新增用户:" + aspnetUsers.getUsername());
	}

	// 查询用户信息
	public UserManagerInfo getUserById(String userId) throws Exception {
		UserManagerInfo userManagerInfo = userManagerMapper
				.getCustomById(userId);
		return userManagerInfo;
	}

	public List<DataTypeIndex> getUserDataList(String userId) throws Exception {
		return null;
	}

	// 查询用户列表
	public List<UserManagerInfo> getUserList(UserManagerInfo userManagerInfo,
			String source) throws Exception {
		// 根据用户名获取当前用户的角色主键
		UserInfo dqUser = new UserInfo();
		dqUser.setUserName(userManagerInfo.getUserName());
		dqUser.setSource(source);
		dqUser = loginMapper.getUserByName(dqUser);
		userManagerInfo.setId(dqUser.getId());
		return userAPIMapper.queryUserList(userManagerInfo);
	}

	public Map<String, Object> getUserOtherPer(String userId) throws Exception {
		return null;
	}

	// 根据用户id查询用户场所权限列表
	public List<SiteAllInfo> getUserSiteList(String userId) throws Exception {
		List<SiteAllInfo> siteList = userManagerMapper
				.querySiteInfoByUser(userId);
		return siteList;
	}

	public void updateUserData(String userId, String dataString)
			throws Exception {
	}

	// 更新用户信息
	public void updateUserInfo(UserManagerInfo userManagerInfo)
			throws Exception {
		// 更新aspnet_Membership表
		AspnetMembership aspnetMembership = new AspnetMembership();
		aspnetMembership.setUserid(userManagerInfo.getUserId());
		aspnetMembership.setMobilepin(userManagerInfo.getPhone());
		aspnetMembership.setEmail(userManagerInfo.getEmail());
		aspnetMembership.setLoweredemail(userManagerInfo.getEmail());
		aspnetMembership.setComment(userManagerInfo.getNote());
		aspnetMembership.setPhoneno(userManagerInfo.getTel());
		aspnetMembership.setRealname(userManagerInfo.getRealName());
		aspnetMembership.setModifytime(new Date());
		aspnetMembershipMapper.updateByPrimaryKeySelective(aspnetMembership);
		// 更新aspnet_UsersInRoles表
		AspnetUsersInRoles aspnetUsersInRoles = new AspnetUsersInRoles();
		aspnetUsersInRoles.setUserid(userManagerInfo.getUserId());
		aspnetUsersInRoles.setRoleid(userManagerInfo.getRoleId());
		aspnetUsersInRoles.setUpdatetime(new Date());
		userManagerMapper.updateUserRole(aspnetUsersInRoles);
	}

	public void updateUserOtherPer(String userId, Map<String, Object> map)
			throws Exception {
	}

	public void updateUserSite(String userId, String siteString)
			throws Exception {
	}

	// 更新用户状态
	public MessageCommon updateUserStatus(String userId, String type)
			throws Exception {
		MessageCommon messageCommon = new MessageCommon();
		messageCommon.setFlag(true);
		AspnetMembership aspnetMembership = new AspnetMembership();
		aspnetMembership.setUserid(userId);
		// 开通
		if ("1".equals(type)) {
			aspnetMembership.setCustomerStatus("1");
		}
		// 锁定
		else if ("2".equals(type)) {
			aspnetMembership.setCustomerStatus("2");
			aspnetMembership.setIslockedout(true);
		}
		// 解锁
		else if ("3".equals(type)) {
			aspnetMembership.setCustomerStatus("1");
			aspnetMembership.setIslockedout(false);
		}
		// 删除
		else if ("4".equals(type)) {
			aspnetMembership.setCustomerStatus("3");
		}
		// 重置密码
		else if ("5".equals(type)) {
			aspnetMembership.setPassword(MD5Util.GetMD5Code("123456"));
		}
		// 恢复
		else if ("6".equals(type)) {
			aspnetMembership.setCustomerStatus("1");
		}
		aspnetMembership.setModifytime(new Date());
		aspnetMembershipMapper.updateByPrimaryKeySelective(aspnetMembership);
		return messageCommon;
	}

}
